<template>
  <div>
    <!-- 搜索 -->
    <!-- 特殊车辆配件类型保修期查询 -->
    <TopFilter ref="TopFilterss" :list="topFilterInit" :cols="3" :collapse="collapse" labelWidth="90" @filterChange="changeHandle"></TopFilter>
    <!-- table -->
    <FilterTable ref="table" :columns="columns" :params="fetchParams" :fetchapi="fetchapi" :datakey="'records'" :onColumnsChange="(columns) => (this.columns = columns)" :is-select-column="false">
      <template slot="controls" slot-scope="props">
        <multiuse-button size="small" type="primary" :click="exportFileBtn.bind()">导出</multiuse-button>
      </template>
    </FilterTable>
    <BaseDialog :visible.sync="visible" :title="dialogTitle" width="850px" destroyOnClose :containerStyle="{ height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px' }">
      <Vehicle @close="close" @save="save"></Vehicle>
    </BaseDialog>
  </div>
</template>
<script>
import Vehicle from '@/pages/claimDemage/tools/ModelVehicle.vue';
import { dictionary } from '@/utils/dictMixin';
import { exportExcelFile } from '@/utils/index';
import { getPart } from '@/api/dictionary/index.js';
import { getListByPage, newSave, piliangqiyong, piliangtingyong, excelExport, editSave, getListForExport,exporturl } from '@/api/claim/dealer/SpecialCarPartTypeWarrantyQuery';
export default {
  mixins: [dictionary],
  components: {
    Vehicle,
  },
  data() {
    return {
      dataList: [],
      visibleDrawer: false,
      collapse: true,
      infoId: null,
      fetchapi: getListByPage,
      topFilterInit: this.topFilterInitFun(),
      flterValue: {}, //参数
      columns: this.creatHeader(),
      fetchParams: {}, //ipa 接口
      selectedRows: [], //选中经销商
      dialogTitle: '选择车辆',
      visibleAdd: false,
      visible: false,
    };
  },
  created() {
    this.chuqi();
  },
  methods: {
    // 导出
   async exportFileBtn() {
      let json = Object.assign({}, { CODE: 'CODE_00000182' }, this.fetchParams);
      let res = await exporturl(json);
      if (res.resultCode == 200) {
        this.$router.push('/admin/auth/oem/downloadCenter');
      }
      // getListForExport(this.fetchParams).then((res) => {
      //   exportExcelFile(res.data, '特殊车辆配件类型保修期');
      // });
    },
    async chuqi() {
      const res = await getPart();
      if (res.resultCode == 200) {
        this.topFilterInit.find(x => x.fieldName == 'typeId').initialValue = '';
        this.topFilterInit.find(x => x.fieldName == 'typeId').itemList = res.data.map(y => ({ text: y.name, value: y.code }));
      }
    },
    //搜索获取数据
    changeHandle(val) {
      this.fetchParams = Object.assign({}, this.fetchParams, val);
      console.log(val);
    },
    save(obj) {
      this.topFilterInit.find((x) => x.fieldName == 'modelId').initialValue = obj[0].modelCode;
      this.visible = false;
    },
    close() {
      this.visible = false;
    },
    chooseOrderNo() {
      this.visible = true;
    },
    //搜索数据
    topFilterInitFun() {
      return [
        { type: 'INPUT', label: 'VIN', fieldName: 'vin', placeholder: '请输入VIN...', initialValue: '' },
        {
          type: 'INPUT',
          label: '车型代码',
          fieldName: 'modelId',
          placeholder: '请输入车型代码...',
          initialValue: '',
          unitRender: (p) => {
            return (
              <div>
                <multiuse-button icon="el-icon-search" onClick={() => this.chooseOrderNo()} />
              </div>
            );
          },
        },
        { type: 'SELECT', label: '配件类型', fieldName: 'typeId', placeholder: '请选择配件类型...', itemList: [], filterable: true},
        {
          type: 'INPUT',
          label: '保修期',
          fieldName: 'rpPeriod',
          unitRender: () => {
            return <span>(月)</span>;
          },
          rules: [
            {
              validator: (rule, value, callback) => {
                if (value) {
                  if (Number.isInteger(Number(value))) {
                    callback();
                  } else {
                    callback(new Error('请输入数字值...'));
                  }
                } else {
                  callback();
                }
              },
              trigger: ['blur', 'change'],
            },
          ],
          placeholder: '请输入保修期...',
          initialValue: '',
        },
        {
          type: 'INPUT',
          label: '保修里程',
          fieldName: 'rpMile',
          unitRender: () => {
            return <span>(KM)</span>;
          },
          rules: [
            {
              validator: (rule, value, callback) => {
                if (value) {
                  if (Number.isInteger(Number(value))) {
                    callback();
                  } else {
                    callback(new Error('请输入数字值...'));
                  }
                } else {
                  callback();
                }
              },
              trigger: ['blur', 'change'],
            },
          ],
          placeholder: '请输入保修里程...',
          initialValue: '',
        },
      ];
    },
    //table表头和数据源
    creatHeader() {
      return [
        {
          title: '序号',
          dataIndex: 'index',
          width: 80,
          sorter: true,
          render: (props) => {
            return <span>{props.row.index + 1}</span>;
          },
        },
        { title: 'VIN', dataIndex: 'vin', sorter: true },
        { title: '车型代码', dataIndex: 'modelId', sorter: true },
        { title: '配件类型', dataIndex: 'typeName', sorter: true },
        { title: '保修期（月）', dataIndex: 'rpPeriod', sorter: true },
        { title: '保修里程（KM）', dataIndex: 'rpMile', sorter: true },
        { title: '创建人', dataIndex: 'createdBy', sorter: true },
        { title: '创建时间', dataIndex: 'createdAt', sorter: true },
        { title: '修改人', dataIndex: 'updatedBy', sorter: true },
        { title: '修改时间', dataIndex: 'updatedAt', sorter: true },
      ];
    },
  },
};
</script>

<style lang="less" scoped></style>
